﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using Hjp.FrameWork.Common;
using Wjtour.BLL;
using Wjtour.Common;

namespace Wjtour.Web.Admin.Ajax
{
    public class PictureHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            var type = context.Request["type"];
            var picid = Utils.StrToInt(context.Request["picid"], 0);
            var title = context.Request["title"];
            var sceneid = Utils.StrToInt(context.Request["sceneid"], 0);

            Wjtour.BLL.sitepicture_bll _sitepicture_bll = new sitepicture_bll();
            Wjtour.BLL.scenepicture_bll _scenepicture_bll = new scenepicture_bll();

            Result r = new Result();
            if (type == "updatetitle")
            {
                Wjtour.Domain.sitepicture pic = _sitepicture_bll.Load(picid);
                pic.picturetitle = title;
                bool b = _sitepicture_bll.Save(pic);
                if (b)
                {
                    r.Errcode = "0";
                    r.Errmsg = "标题更新成功！";
                }
            }
            else if (type == "setcover")
            {
                bool b = _sitepicture_bll.SetCover(picid) > 0;
                if (b)
                {
                    r.Errcode = "0";
                    r.Errmsg = "封面更新成功！";
                }

            }
            else if (type == "removepic")
            {
                bool b = _sitepicture_bll.BatchDelete(picid.ToString()) > 0;
                if (sceneid > 0)
                {
                    _scenepicture_bll.Delete(sceneid, picid);
                }
                if (b)
                {
                    r.Errcode = "0";
                    r.Errmsg = "图片删除成功！";
                }
            }
            else {
                r.Errcode = "999";
                r.Errmsg = "操作失败！";
            }
            JavaScriptSerializer serialzier = new JavaScriptSerializer();
            string output = serialzier.Serialize(r);
            context.Response.Write(output);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
